package com.thinkwide.data.login;

import com.thinkwide.data.core.MFString;
import com.thinkwide.data.filter.MFXFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MFLogin {
    public static final String userkey = "@user";
    public static final String admin = "admin";
    public static final String boss = "boss";
    public static final String normal = "normal";
    public static final String departmanager = "departmanager";
    public static final String superadmin = "superadmin";

    public static final String login = "login";


    public static HttpSession getSession() {
        HttpServletRequest request = MFXFilter.threadRequestLocal.get();
        return request.getSession();
    }

    public static HttpServletRequest getRequest() {
        HttpServletRequest request = MFXFilter.threadRequestLocal.get();
        return request;
    }

    public static HttpServletResponse getResponse() {
        HttpServletResponse response = MFXFilter.threadResponseLocal.get();
        return response;
    }

    public static boolean loginOut() {
        HttpServletRequest request = MFXFilter.threadRequestLocal.get();
        request.getSession().removeAttribute(userkey);
        return true;
    }

    public static boolean isAdmin() {
        if (!MFLogin.hasLogin())
            return false;
        else
            return (MFLogin.getUserType() == MFUserType.admin || MFLogin.getUserType() == MFUserType.superadmin);

    }

    public static boolean isSuperAdmin() {
        return (MFLogin.getUserType() == MFUserType.superadmin);
    }

    public static boolean hasLogin() {
        HttpServletRequest request = MFXFilter.threadRequestLocal.get();
        Object obj = null;
        if (request != null) obj = request.getSession().getAttribute(userkey);
        return obj != null;
    }

    public static MFUser getUser() {
        HttpServletRequest request = MFXFilter.threadRequestLocal.get();
        MFUser user = (MFUser) request.getSession().getAttribute(userkey);
        return user;
    }

    public static MFUserType getUserType() {

        return getUser().getUserType();
    }

    public static boolean checkUser(String userId, String passWord) {
        MFUser user = MFLogin.getUserFromDB(userId);

        if (user != null) {
            return MFString.compareCase(user.getPassword(), passWord);
        }

        return false;
    }

    public static boolean loginUser(String userId, String type) {
        MFBaseLogin log = new MFBaseLogin();
        MFUser user = log.getLoginUser(userId);
        if (user == null) return false;
        if (log.login(userId)) return false;

        HttpServletRequest request = MFXFilter.threadRequestLocal.get();
        request.getSession().setAttribute(userkey, user);
        log.logData(userId, type);
        return true;
    }

    public static boolean loginUser(String userId, String passWord, String type) {
        MFBaseLogin log = new MFBaseLogin();
        MFUser user = log.getLoginUser(userId);
        if (user == null) return false;
        if (!MFString.compareCase(user.getPassword(), passWord)) return false;

        if (!log.login(userId)) return false;

        HttpServletRequest request = MFXFilter.threadRequestLocal.get();
        request.getSession().setAttribute(userkey, user);
        log.logData(userId, type);
        return true;

    }

    public static MFUser getUserFromDB(String userId) {
        //String sBrowser=MFSystem.getBrowser(request);
        //String sIP=MFSystem.getIpAddr(request);
        //String sUserID=getUserId();
        //MFDBLog.logData(sUserID, sBrowser, stype, sIP,stype);
        MFBaseLogin log = new MFBaseLogin();
        MFUser user = log.getLoginUser(userId);
        return user;
    }


    public static String getLanguage() {
        String str = "";
        if (getUser() != null)
            str = getUser().getLanguage();
        else
            str = MFString.Empty;

        if (MFString.isNullOrEmpty(str))
            return "zh";
        else
            return str;
    }

    public static String getLoginId() {
        if (getUser() != null)
            return getUser().getLoginId();
        else
            return MFString.Empty;

    }

    public static String getUserId() {
        if (getUser() != null)
            return getUser().getId();
        else
            return MFString.Empty;

    }

    public static String getUserName() {
        if (getUser() != null)
            return getUser().getName();
        else
            return MFString.Empty;

    }

}
